package com.yonyou.brigade.openapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.openapi.service.IApplySyncService;
import com.yonyou.ybzboot.starter.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RequestMapping("/open/apply/")
@Controller
public class ApplySyncController {

    @Autowired
    private IApplySyncService applySyncService;

    /**
     * 申请单同步
     * @param param
     * @param request
     * @return
     */
    @RequestMapping("/sync")
    @ResponseBody
    public JSONObject applySync(@RequestBody JSONObject param, HttpServletRequest request) {
        String phone = request.getParameter("phone");
        String tenantId = request.getParameter("tenantId");
        String token = request.getParameter("token");
        if (StringUtils.isEmpty(phone)) {
            throw new BusinessException("手机号不能为空");
        }
        if (StringUtils.isEmpty(tenantId)) {
            throw new BusinessException("租户id不能为空");
        }
        if (StringUtils.isEmpty(token)) {
            throw new BusinessException("token不能为空");
        }
        if (param == null) {
            throw new BusinessException("请检查body参数");
        }
        if (StringUtils.isEmpty(param.getString("billNum"))) {
            throw new BusinessException("单据编号不能为空");
        }
        if (com.yonyou.brigade.util.operateutil.StringUtils.generateJudgment(token)) {
            throw new BusinessException("token验证不通过");
        }
        return applySyncService.applySync(phone, tenantId, param);
    }

    @RequestMapping("/queryApplyInfo")
    @ResponseBody
    public Map queryApplyParam(@RequestBody JSONObject param, HttpServletRequest request) {
        String phone = request.getParameter("phone");
        String tenantId = request.getParameter("tenantId");
        String token = request.getParameter("token");
        if (StringUtils.isEmpty(phone)) {
            throw new BusinessException("手机号不能为空");
        }
        if (StringUtils.isEmpty(tenantId)) {
            throw new BusinessException("租户id不能为空");
        }
        if (StringUtils.isEmpty(token)) {
            throw new BusinessException("token不能为空");
        }
        if (StringUtils.isEmpty(param.getString("billNum"))) {
            throw new BusinessException("单据编号不能为空");
        }
        if (StringUtils.isEmpty(param.getString("key"))) {
            throw new BusinessException("key不能为空");
        }
        if (com.yonyou.brigade.util.operateutil.StringUtils.generateJudgment(token)) {
            throw new BusinessException("token验证不通过");
        }
        if (StringUtils.isEmpty(param.getString("channel"))) {
            throw new BusinessException("channel不能为空");
        }
        return applySyncService.queryApplyInfo(phone, tenantId, param);
    }
}
